package com.jic.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.model.entity.CustomerConsumeSts;
import com.jic.data.model.vo.request.CustomerConsumeStsRequest;
import com.jic.data.model.vo.request.CustomerPlatformStsRequest;
import com.jic.data.model.vo.response.CustomerConsumeStsResponse;
import com.jic.data.service.CustomerConsumeStsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-05-13 17:20:54
 */
@RestController
@Slf4j
@RequestMapping("/customerConsumeSts")
@Api(tags = " [客户购买统计]控制类-PC")
public class CustomerConsumeStsController {
    @Autowired
    private CustomerConsumeStsService service;

    /**
     * 新增记录
     *
     * @param request 新增[客户购买统计]请求参数
     * @return CustomerConsumeStsResponse 新增[客户购买统计]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[客户购买统计]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody CustomerConsumeStsRequest request) throws Throwable {
        CustomerConsumeSts customerConsumeSts = new CustomerConsumeSts();
        BeanUtils.copyProperties(request, customerConsumeSts);
        long flag = service.save(customerConsumeSts);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[客户购买统计]请求参数
     * @return CustomerConsumeStsResponse 新增或修改[客户购买统计]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[客户购买统计]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody CustomerConsumeStsRequest request) throws Throwable {
        CustomerConsumeSts customerConsumeSts = new CustomerConsumeSts();
        BeanUtils.copyProperties(request, customerConsumeSts);
        long flag = service.saveOrUpdate(customerConsumeSts);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[客户购买统计]请求参数
     * @return CustomerConsumeStsResponse 删除[客户购买统计]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[客户购买统计]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody CustomerConsumeStsRequest request) throws Throwable {
        CustomerConsumeSts customerConsumeSts = new CustomerConsumeSts();
        BeanUtils.copyProperties(request, customerConsumeSts);
        long flag = service.deleteByPrimaryKeyLogically(customerConsumeSts);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[客户购买统计]请求参数
     * @return CustomerConsumeStsResponse 修改[客户购买统计]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[客户购买统计]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody CustomerConsumeStsRequest request) throws Throwable {
        CustomerConsumeSts customerConsumeSts = new CustomerConsumeSts();
        BeanUtils.copyProperties(request, customerConsumeSts);
        long flag = service.updateByPrimaryKeySelective(customerConsumeSts);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[客户购买统计]详情请求参数
     * @return CustomerConsumeStsResponse 查询[客户购买统计]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[客户购买统计]详情")
    @ResponseBody
    public RestResult<CustomerConsumeStsResponse> getByPrimaryKey(@Validated @RequestBody CustomerConsumeStsRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            CustomerConsumeSts customerConsumeSts = new CustomerConsumeSts();
            BeanUtils.copyProperties(request, customerConsumeSts);
            CustomerConsumeSts result = service.getByPrimaryKey(customerConsumeSts);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            CustomerConsumeStsResponse bo = new CustomerConsumeStsResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new CustomerConsumeStsResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<CustomerConsumeSts>> listPage(@RequestBody CustomerConsumeStsRequest request) throws Throwable {
        CustomerConsumeSts param = new CustomerConsumeSts();
        BeanUtils.copyProperties(request, param);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }

    /**
     * CUSTOMER_CONSUME_STS 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [客户购买统计] ")
    @ResponseBody
    public RestResult<List<CustomerConsumeStsResponse>> listCustomerConsumeStsAllRecord(@RequestBody CustomerConsumeStsRequest request) throws Throwable {
        CustomerConsumeSts customerConsumeSts = new CustomerConsumeSts();
        BeanUtils.copyProperties(request, customerConsumeSts);
        List<CustomerConsumeSts> list = service.listAllRecord(customerConsumeSts);
        List<CustomerConsumeStsResponse> responseList = list.stream().map(temp -> {
            CustomerConsumeStsResponse bo = new CustomerConsumeStsResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 查询订单数
     *
     * @param request 前端请求参数
     */
    @PostMapping("/secOrderNum")
    @ApiOperation(value = "查询订单数")
    @ResponseBody
    public RestResult<Long> secOrderNum(@RequestBody CustomerConsumeStsRequest request) throws Throwable {

        return RestResult.success(service.secOrderNum(request));
    }

    /**
     * 查询商品数量
     *
     * @param request 查询商品数量
     */
    @PostMapping("/secProductNum")
    @ApiOperation(value = "查询商品数量")
    @ResponseBody
    public RestResult<Long> secProductNum(@RequestBody CustomerConsumeStsRequest request) throws Throwable {

        return RestResult.success(service.secProductNum(request));
    }

    /**
     * 查询销售额
     *
     * @param request 查询销售额
     */
    @PostMapping("/secSaleVolume")
    @ApiOperation(value = "查询销售额")
    @ResponseBody
    public RestResult<Double> secSaleVolume(@RequestBody CustomerConsumeStsRequest request) throws Throwable {

        return RestResult.success(service.secSaleVolume(request));
    }

    /**
     * 查询折扣率
     *
     * @param request 查询折扣率
     */
    @PostMapping("/secDiscountRate")
    @ApiOperation(value = "查询折扣率")
    @ResponseBody
    public RestResult<Double> secDiscountRate(@RequestBody CustomerConsumeStsRequest request) throws Throwable {

        return RestResult.success(service.secDiscountRate(request));
    }





}

